//
//  HeaderView.m
//  Marrymemo
//
//  Created by dllo on 15/8/8.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "PicView.h"
#import "HomeModel.h"
#import "DKNightVersion.h"
#import "UIImageView+NightImage.h"
#import "UIImage+WebP.h"
#import "CustomTap.h"

@interface PicView ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, assign) int timeCount;
@property (nonatomic, assign) int count;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, assign) NSInteger arrayCount;

@end

@implementation PicView

-(void)dealloc{
   
    [_url release];
    [_scrollView release];
    [_pageControl release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [DKNightVersionManager addClassToSet:self.class];
        self.nightBackgroundColor = UIColorFromRGB(0x343434);
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:_scrollView];
        
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((frame.size.width - 100) / 2, frame.size.height - 20, 100, 20)];
         [self addSubview:self.pageControl];
       
        self.pageControl.numberOfPages = 17;
        [self createSubView];
       

    }
    return self;
}

- (void)createSubView
{
    
    _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    [_scrollView release];
    
    
    self.pageControl.currentPage = 0;
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    [_pageControl release];
}

#pragma mark -
#pragma mark model传值
-(void)valueWithArray:(NSArray *)picName
{
    
   self.pageControl.numberOfPages = picName.count - 2;
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * picName.count, 0);
    self.arrayCount = picName.count;
    for (int i = 0; i < picName.count; i++) {
        
        NSDictionary *dic = [picName objectAtIndex:i];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * _scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height - 60)];
        imageView.userInteractionEnabled = YES;
        [_scrollView addSubview:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"pic"]]];
    
        [imageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"pic"]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            imageView.nightImage = image;
        }];
        
        [imageView release];
        
        CustomTap *tap = [[CustomTap alloc] initWithTarget:self action:@selector(Click:)];
        tap.dic = dic;
        [imageView addGestureRecognizer:tap];
        [tap release];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.size.height + imageView.frame.origin.y + 5, [[UIScreen mainScreen] bounds].size.width, 30)];
        label.text = [dic objectForKey:@"title"];
        label.nightTextColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        [self.scrollView addSubview:label];
        [label release];
        
    }
    _count = 0;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    self.timeCount = (int)picName.count - 2;
    
}
- (void)Click:(CustomTap *)tap
{
    
    [self.delegate valueWithUrl:[tap.dic objectForKey:@"url"]];
    
    
}

- (void)nextImage
{
    
    
     if (self.count < self.timeCount) {
            
//        self.scrollView.contentOffset = CGPointMake((self.count + 2) * [[UIScreen mainScreen] bounds].size.width, 0);
        self.count++;
        if (self.count >= self.timeCount) {
            
            self.count = 0;
          
            
        }
         self.pageControl.currentPage = self.count;
         
         self.scrollView.contentOffset = CGPointMake((self.count + 1) * [[UIScreen mainScreen] bounds].size.width, 0);
        self.pageControl.currentPage = self.count;
            
         

    }
   
    
}
#pragma mark -
#pragma mark 图片翻页
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (scrollView.contentOffset.x >= (self.arrayCount - 1) * self.scrollView.frame.size.width) {
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width , 0)];
    
    }
    if (scrollView.contentOffset.x <= 0) {
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * (self.arrayCount - 2) , 0)];
    
  }
    
    int halfX = scrollView.frame.size.width / 2;
    self.count = (scrollView.contentOffset.x - self.scrollView.frame.size.width - halfX) / scrollView.frame.size.width + 1;
    self.pageControl.currentPage = self.count;
    
}



@end
